package com.ly.cloud.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.ClassroomDto;
import com.ly.cloud.entity.ClassroomPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.biz.BusinessException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.ClassroomMapper;
import com.ly.cloud.service.ClassroomService;
import com.ly.cloud.vo.ClassroomTypeVo;
import com.ly.cloud.vo.ClassroomVo;

/**
 * @Description:
 * @Author: luhuacong
 * @Date: Create in 2018/12/12
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public class ClassroomServiceImpl implements ClassroomService {

	@Autowired
	private ClassroomMapper classroomMapper;
	/** 
	 * Method Name:  insertClassroom
	 * Description:  
	 * 添加教室信息
	 * @param dto
	 * @param createMan 创建人
	 * @author LuHuaCong
	 * @date: 2018年12月14日
	 */
	@Override
	public boolean insertClassroom(ClassroomDto dto,String createMan) {
		try {
			ClassroomPo po = new ClassroomPo();
			BeanUtils.copyProperties(dto, po);
			/**
			 * 设置创建人
			 */
			po.setCzj(createMan);
			/**
			 * 创建时间
			 */
			po.setCjsj(new Date());
			Integer i = classroomMapper.insert(po);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (BeansException e) {
			e.printStackTrace();
			throw new DBException("添加教室信息失败！", e);

		}
	}
	
	/** 
	 * Method Name: deleteClassroomByid
	 * Description:  
	 * 根据id删除教室信息
	 * @param id 教室编号
	 * @author LuHuaCong
	 * @date: 2018年12月14日
	 */
	@Override
	public boolean deleteClassroomByid(String id) {
		try {
			Integer i = classroomMapper.deleteById(id);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			throw new DBException("删除教室信息失败！", e);
		}		
	}

	/** 
	 * Method Name: deleteClassroomByid
	 * Description:  
	 * 批量删除教室信息
	 * @param ids 教室编号集合
	 * @author LuHuaCong
	 * @date: 2018年12月14日
	 */
	@Override
	public Integer deleteClassroomByIds(List<String> ids) {
		try {
			Integer i = classroomMapper.deleteBatchIds(ids);
			return i;
		} catch (Exception e) {
			throw new DBException("批量删除教室信息失败！", e);
		}
	}

	/** 
	 * Method Name: updateClassroom
	 * Description:  
	 * 修改教室信息
	 * @param dto
	 * @param modifyMan 修改人
	 * @author LuHuaCong
	 * @date: 2018年12月14日
	 */
	@Override
	public boolean updateClassroom(ClassroomDto dto, String modifyMan) {
		try {
			ClassroomPo po = new ClassroomPo();
			BeanUtils.copyProperties(dto, po);
			//设置修改时间
			po.setXgsj(new Date());
			
			//设置修改人
			po.setXgr(modifyMan);
			Integer i = classroomMapper.updateById(po);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (BeansException e) {
			e.printStackTrace();
			throw new BusinessException("修改教室信息失败！", e);
		}
	}

	/** 
	 * Method Name:selectByCondition
	 * Description:  根据条件 查询教室信息
	 * @param page 
	 * @return Page<ClassroomVo>
	 * @author LuHuaCong
	 * @date: 2018年12月14日
	 */
	@Override
	public Page<ClassroomVo> selectByCondition(Page<ClassroomVo> page, ClassroomDto dto) {
		try {	
			Page<ClassroomVo> records = page.setRecords(classroomMapper.selectJsByCondition(page, dto));
			return records ;
		} catch (Exception e) {
			throw new CloudException("教室信息就显示异常，请重试!", e);
		}
	}

	/** 
	 * Description:教室类型下拉列表
	 * @return 教室类型信息
	 * @author LuHuaCong
	 * @date: 2018年12月17日
	 */
	@Override
	public List<ClassroomTypeVo> selectClaType() {
		try {
			return classroomMapper.selectClaType();
		} catch (Exception e) {
			throw new CloudException("教室类型查询异常,请重试!", e);
		}
	}

}
